/**
 * File    : LocaleSelectModel.java
 * Created : 2008-10-16
 * By      : LuHuiguo
 */
package com.luhuiguo.tapestry.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.tapestry5.OptionGroupModel;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.util.AbstractSelectModel;

/**
 * @author LuHuiguo
 * @version $Id: LocaleSelectModel.java 2 2008-10-18 04:37:52Z LuHuiguo $
 * 
 */
public class LocaleSelectModel extends AbstractSelectModel {

	private String locales;

	public LocaleSelectModel(String locales) {
		this.locales = locales;
	}

	@Override
	public List<OptionGroupModel> getOptionGroups() {
		return null;
	}

	@Override
	public List<OptionModel> getOptions() {
		List<OptionModel> options = new ArrayList<OptionModel>();

		if (locales != null) {
			String[] locs = locales.split(",");
			for (String str : locs) {
				options.add(new LocaleOptionModel(LocaleEncoder.toLocale(str)));
			}
		} else {
			Locale[] locales = Locale.getAvailableLocales();
			for (Locale locale : locales) {
				options.add(new LocaleOptionModel(locale));
			}
		}
		return options;
	}

}
